<template>
	<view class="">
		<view class="" v-if="list.length >0">
    
      <u-radio-group v-model="isdefault" @change="radioGroupChange">
      	<view class="flex flex-column w-100">
          <block v-for="(item,index) in list" :key="index">
            <view class="flex align-center justify-between u-p-x-30 u-p-y-20 uborder-bottom w100" >
              <u-radio :name="item.address_id" :key="index">
                <view class="flex-1 u-p-l-20">
                	<view class="flex">
                		<text>{{item.name}}</text>
                		<text class="u-m-l-20 u-m-r-40">{{item.phone}}</text>
                		<view class="u-p-x-20 bg-warning rounded-10" v-if="item.isdefault*1">
                			<text class=" text-white ">默认</text>
                		</view>
                	</view>
                	<view class="flex-1 u-m-t-10 text-ellipsis-2" style="width: 500rpx;">
                		<text>收货地址: {{item.Area.province+','+item.Area.city+','+item.Area.region+','+item.detail}}</text>
                	</view>
                </view>
            	</u-radio>
            	<u-icon @click="toEditPage(item.address_id)" name="edit-pen" size="40"></u-icon>
            </view>
          </block>
      	</view>
      </u-radio-group>
		</view>
		<view class="flex flex-column align-center u-p-y-50" v-else>
			<image src="../static/mine/no-address.png" style="width: 166rpx;" mode="widthFix"></image>
			<text class="u-m-t-30">亲，您还没收货地址哦，赶快添加吧！</text>
		</view>
		<view class="position-fixed left-0 right-0" style="bottom: 100rpx;">
			<dy-button size="default" width="576rpx" @click="toPage">添加收货地址</dy-button>
		</view>
	</view>
</template>

<script>
	import { BaseCenterApi } from '@/api/serves.js'
	export default {
		data() {
			return {
        from: '', // 从哪里跳转过来的
				list: [],
				isdefault: ''
			}
		},
		onShow() {
			this._addressList()
		},
    onLoad(options) {
      this.from = options.from
    },
		methods: {
			_addressList() {
				this.$ShowLoading()
				BaseCenterApi.addressList().then(res=> {
					this.$HideLoading()
					console.log('地址列表', res);
					if(res.code == 1) {
						this.list = res.data.list
						for (let item of this.list) {
							if(item.isdefault == '1') {
								this.isdefault = item.address_id
								break
							}
						}
					}
				})
			},
			radioGroupChange(id) {
				this.$ShowLoading()
				BaseCenterApi.defaultaddress({ id }).then(res=> {
					this.$HideLoading()
					console.log('设置默认地址', res);
					if(res.code == 1) {
						this._addressList()
            if(this.from == 'orderAdd') {
              uni.$emit('addressClick')
              uni.navigateBack()
            }
					}
				})
			},
			toPage() {
				uni.navigateTo({
					url: '/pages/mine/settingInfo/settingAddressEdit/settingAddressEdit?info=' + JSON.stringify({type: 'add'})
				})
			},
			toEditPage(id) {
				uni.navigateTo({
					url: '/pages/mine/settingInfo/settingAddressEdit/settingAddressEdit?info=' +  JSON.stringify({type: 'edit', id: id})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
page {
	background-color: #FFFFFF;
}
</style>
